/**
 * 给定一个二叉树，判断它是否是
 * 平衡二叉树
 *方法二
 */



class Solution2 {
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        return getHeight(root) >= 1;
    }

    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        if(leftHeight < 0){
            return -1;
        }
        int rightHeight = getHeight(root.right);
        if(rightHeight < 0){
            return -1;
        }

        if(Math.abs(leftHeight - rightHeight) <= 1){
            return Math.max(leftHeight,rightHeight) + 1;
        }else{
            return -1;
        }
    }
}